package co.edu.unal.scrum.client.productbacklog;

import co.edu.unal.scrum.client.gxt.ColumnFactory;
import co.edu.unal.scrum.client.resources.Resources;
import co.edu.unal.scrum.shared.model.Product;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.IconButton;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;

public class ProductBackLogView extends ViewWithUiHandlers<ProductBackLogUiHandlres> implements ProductBackLogPresenter.MyView {
private static final BeanModelFactory factory = BeanModelLookup.get().getFactory(Product.class);
	
	private LayoutContainer layoutContainer;
	private ContentPanel gridPanel;
	
	private Grid<BeanModel> grid;
	private ColumnModel columnModel;
	private ListStore<BeanModel> store;
	
	private Button add;
	private Button delete;
	
	private ToolBar actionToolBar;

	private ColumnConfig idColumn;

	private ColumnConfig statusColumn;

	private ColumnConfig userHistory;

	private ColumnConfig storyPoints;

	private ColumnConfig editColumn;	@Inject
	public ProductBackLogView(final Resources resources) {
		layoutContainer = new LayoutContainer();
		layoutContainer.setId(ProductBackLogView.class.getName());
		layoutContainer.setLayout(new BorderLayout());
		
		store = new ListStore<BeanModel>();
		
		 idColumn = ColumnFactory.manufactureColumn("id","id");
		 idColumn.setWidth(5);
	    statusColumn=ColumnFactory.manufactureColumn("priority", "Priority");
		 statusColumn.setWidth(6);

	    userHistory=ColumnFactory.manufactureColumn("history", "User History");
	    storyPoints=ColumnFactory.manufactureColumn("points", "Story Points");
		 storyPoints.setWidth(7);


	    
	    
	    
	    GridCellRenderer<BeanModel> editButtonRenderer = new GridCellRenderer<BeanModel>() {
			@Override
			public Object render(BeanModel model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<BeanModel> store, Grid<BeanModel> grid) {
				IconButton editButton = new IconButton(resources.style().iconPencilClass(), new SelectionListener<IconButtonEvent>() {
					@Override
					public void componentSelected(IconButtonEvent ce) {
						
					}
				});
				return editButton;
			}
	    };
	    
	    editColumn =  ColumnFactory.manufactureDateColumn("editColumn", "Editar");
	    editColumn.setRenderer(editButtonRenderer);
	    editColumn.setWidth(5);
	    
		columnModel = ColumnFactory.manufactureColumnModel(idColumn, statusColumn, userHistory,statusColumn,storyPoints, editColumn);
		
		gridPanel = new ContentPanel(new FitLayout());
		gridPanel.setIcon(AbstractImagePrototype.create(resources.iconLayoutContent()));
		gridPanel.setHeading("Product Backlog");
		
	    grid = new Grid<BeanModel>(store, columnModel);
	    grid.setAutoExpandColumn("id");
	    grid.setColumnLines(true);
	    grid.getSelectionModel().setSelectionMode(SelectionMode.MULTI);
	    grid.setBorders(true);
	    grid.getView().setForceFit(true);
	    gridPanel.setScrollMode(Scroll.AUTOY);
	    gridPanel.add(grid);
	    grid.setHeight("177px");
	    
	    add = new Button("Add History", AbstractImagePrototype.create(resources.iconAdd()));
	    add.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				if(getUiHandlers()!=null){
					getUiHandlers().addUserHistory();
				}
			}
		});
		delete = new Button("Delete History", AbstractImagePrototype.create(resources.iconDelete()));
		
		actionToolBar = new ToolBar();
		actionToolBar.add(add);
		actionToolBar.add(new SeparatorToolItem());
		actionToolBar.add(delete);
	    
		gridPanel.setTopComponent(actionToolBar);

		layoutContainer.add(gridPanel, new BorderLayoutData(LayoutRegion.CENTER));
	}

	@Override
	public Widget asWidget() {
		return layoutContainer;
	}
}
